/**
 * Combo插件扩展 
 * 基于jQuery 1.8.0、EasyUI 1.3.2开发
 * 参照EasyUI 1.5.4.2的属性、事件与方法进行扩展
 * 向前兼容处理
 * @version 1.0
 * @author 范围兄 <ambit_tsai@qq.com>
 * @requires jQuery 1.8.0 or above
 * @requires EasyUI 1.3.2 or above
 * @see https://gitee.com/ambit/easyui_exts/blob/master/combo.md
 */   
;(function($){
	'use strict';
	
	var plugin = 'combo';
	
	if(typeof $.fn[plugin]!=='function' || $.type($.fn[plugin].defaults)!=='object'){
		throw 'jQuery.fn.'+plugin+' is invalid';
	}
	

	// 代理插件解释器 
	var fn = $.fn[plugin];
	$.fn[plugin] = function(options, param){
		if(typeof options === 'string')
			return $.fn[plugin].__fn.call(this, options, param);

		return this.each(function(){
			var jq = $(this), onAfterCreate,
				defs = $.fn[plugin].__fn.defaults, 
				opts = $.extend(true, 	// 解析得到配置选项 
					{}, 
					$.fn[plugin].defaults, 
					$.fn[plugin].parseOptions(this), 
					options || {}
				);

			if( !defs.hasOwnProperty('onBeforeCreate') ){
				opts.onBeforeCreate.call(jq);	// 触发onBeforeCreate事件 
				delete opts.onBeforeCreate;	// 阻止传递给其他扩展，导致重复触发
			}
			if( !defs.hasOwnProperty('onAfterCreate') ){
				onAfterCreate = opts.onAfterCreate;
				delete opts.onAfterCreate;	// 阻止传递给其他扩展，导致重复触发
			}

			$.fn[plugin].__fn.call(jq, opts, param);
			opts = jq.data(plugin).options;	// 重新获取配置选项 

			// 解释扩展的属性与事件 
			var defs = $.fn[plugin].__fn.defaults, 
				state = jq.data(plugin), 
				$input = state.combo.find('input.validatebox-text'), 
				$panel = state.panel;
			opts = state.options;	// 重新获取配置选项 

			if(!defs.hasOwnProperty('panelMinWidth') && opts.panelMinWidth){
				$panel.css('min-width', opts.panelMinWidth);
			}
			if(!defs.hasOwnProperty('panelMaxWidth') && opts.panelMaxWidth){
				$panel.css('max-width', opts.panelMaxWidth);
			}
			if(!defs.hasOwnProperty('panelMinHeight') && opts.panelMinHeight){
				$panel.css('min-height', opts.panelMinHeight);
			}
			if(!defs.hasOwnProperty('panelMaxHeight') && opts.panelMaxHeight){
				$panel.css('max-height', opts.panelMaxHeight);
			}

			$input.on('keydown.ambit_combo', function(event){
				switch(event.which){
					case 37:
						opts.keyHandler.left.call(jq[0], event);
						break;
					case 39:
						opts.keyHandler.right.call(jq[0], event);
						break;
				}
			});

			if(!defs.hasOwnProperty('autoShowPanel') && opts.autoShowPanel){
				$input.on('click.ambit_combo', function(){
					jq.combo('showPanel');
				});
			}
			if( onAfterCreate ){
				onAfterCreate.call(jq);	// 触发onAfterCreate事件 
			}
		});
	};
	$.extend($.fn[plugin], fn);
	$.fn[plugin].__fn = fn;


	// 扩展属性与事件 
	$.fn[plugin].defaults = $.extend(true, {
		panelMinWidth: null,
		panelMaxWidth: null,
		panelMinHeight: null,
		panelMaxHeight: null,
		multivalue: true,
		readonly: false,	// 可从ambit.easyui.validatebox.js继承 
		keyHandler: {
			left: function(e){},
			right: function(e){}
		},
		autoShowPanel: false,
		onBeforeCreate: function(){},
		onAfterCreate: function(){}
	}, fn.defaults);


	// 扩展方法 
	$.fn[plugin].methods = $.extend({
		//
	}, fn.methods);

	if( !fn.defaults.hasOwnProperty('multivalue') ){
		$.extend($.fn[plugin].methods, {
			getValues: function(jq){
				var opts = $.data(jq[0], 'combo').options, 
					vals =  $.fn.combo.__fn.methods.getValues(jq);
				if( opts.multivalue ){
					return vals;
				}else if(!vals.length || !vals[0]){
					return [];
				}else{
					return vals[0].split( opts.separator );
				}
				
			}, 
			setValues: function(jq, vals){
				return jq.each(function(){
					var $this = $(this);
					if( $this.data('combo').options.multivalue ){
						$.fn.combo.__fn.methods.setValues($this, vals);
					}else{
						$this.combo('setValue', [vals.toString()]);
					}
				});
			}
		});
	}
	
}(jQuery));